# -*- codeing:UTF-8 -*_
'''
@Date   :2023/11/12
@Time   :16:08
@Author :RussellZhou
@Desc   :三国杀武将图片
'''
import os
from bs4 import BeautifulSoup
import requests

url = 'https://www.sanguosha.cn/pc/hero-list.html'
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"}
res=requests.get(url=url,headers=header)
html=res.text
soup=BeautifulSoup(html,'html.parser')
img_list=soup.findAll('img')
file_path='sgs/iconImg/'
if os.path.exists(file_path):
    if len(os.listdir(file_path))==0:
        os.rmdir(file_path)
else:
    os.mkdir(file_path)
for i in range(16,len(img_list)):
    img_name=img_list[i]['alt']
    img_url=img_list[i]['src']
    img_data=requests.get(url=img_url,headers=header).content
    img_path='sgs/iconImg/'+str(img_name)+".jpg"
    with open(img_path,'wb') as f:
        f.write(img_data)